﻿<html>
<head>
    <title>业务维护</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <link href="/styles/styles.css" type="text/css" rel="stylesheet"/>

    <link rel="stylesheet" type="text/css" href="/style/public.css">
    <link rel="stylesheet" type="text/css" href="/style/common.css">
    <link rel="stylesheet" type="text/css" href="/vendor/jquery/easyui/themes/metro/metro-blue/easyui.css">
    <link rel="stylesheet" type="text/css" href="/vendor/jquery/easyui/themes/icon.css">
    <script type="text/javascript" src="/js/plugin/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="/vendor/jquery/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/vendor/jquery/easyui/extend/datagrid-filter.js"></script>
    <script type="text/javascript" src="/js/common/common.js">

    </script>
    <style>
        .icon-filter {
            background: url('/vendor/jquery/easyui/extend/filter.png') no-repeat center center;
        }
    </style>

</head>
<body>
<div class=''>
    <table id="dg" class="easyui-datagrid" title="维度转换表" style="width:1400px"
           data-options="
            rownumbers:true
           ,checkOnSelect:true
           ,selectOnCheck:false
           ,autoRowHeight:false
           ,singleSelect:true
           ,fitColumns:true

           ,method:'get'
           ,toolbar:'#tb'
           ,url: '/dimTrans/getAllListInPage'
           ,onClickRow: onClickRow
           ,onUnselect:saveOrUpdateRow
           ,pagination: true
           ,pagePosition:'both'
           ,pageList:[20]
           ,rowStyler: function(index,row){
                                if (index%2==0){return 'background-color:#E6FDEE;color:#fff;'; }
                             }
           ,pageSize: 20
           ">
        <thead>
        <tr>
            <th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'old_table_nm'      ,width:250,align:'left',editor:'text'">原表名</th>
            <th data-options="field:'old_column_nm1' ,width:100,align:'left',editor:'text'">原字段名1</th>
            <th data-options="field:'old_key_id1'         ,width:100,align:'left',editor:'text'">原枚举值1</th>
            <th data-options="field:'old_key_nm1'       ,width:100,align:'left',editor:'text'">原枚举名1</th>
            <th data-options="field:'new_table_nm'     ,width:250,editor:'text'">新表名</th>
            <th data-options="field:'new_column_nm' ,width:100,align:'left',editor:'text'">新字段名</th>
            <th data-options="field:'new_key_id'         ,width:100,align:'left',editor:'text'">新枚举值</th>
            <th data-options="field:'new_key_nm'       ,width:100,align:'left',editor:'text'">新枚举名</th>
            <th data-options="field:'biz_id'                       ,width:150,align:'center',formatter:getValue
            ,editor:{
							type:'combobox',
							options:{
								 valueField:'biz_id'
								,textField:'value'
								,method:'get'
								,width:150
								,align:'center'
								,url:'/biz/getAllBizListNm'
								,required:true
					    }
					 }
            ">业务
            </th>
        </tr>
        </thead>
    </table>
    <script>
        $(function () {
            var pager = $('#dg').datagrid('getPager');	// get the pager of datagrid
            console.log(pager);
            pager.pagination({
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '第{from}到{to}条，共{total}条'
            });
        } );
    </script>
    <script>


        $(function () {

            var dg = $('#dg').datagrid({
                filterBtnIconCls: 'icon-filter'
                , remoteFilter: true
                , filterPosition: top
                ,showFilterBar:true
            });

            dg.datagrid('enableFilter', [
                {
                    field: 'new_column_nm',
                    type: 'combobox',
                    options: {
                        url: '/dimTrans/getNewColumnNameList',
                        method: 'get',
                        prompt: '新字段名',
                        width: 150,
                        valueField: 'name',
                        textField: 'name',
                        panelHeight: 'auto',
                        onChange: function (value) {
                            
                            if (value == '') {
                                dg.datagrid('removeFilterRule', 'new_column_nm');
                            } else {
                                dg.datagrid('addFilterRule', {
                                    field: 'new_column_nm',
                                    op: 'equal',
                                    value: value
                                });
                            }
                            dg.datagrid('doFilter');
                        }
                    },
                    op: 'equal'
                },
                {
                    field: 'biz_id',
                    type: 'combobox',
                    options: {
                        url: '/dimTrans/getBizList',
                        method: 'get',
                        prompt: '业务',
                        width: 150,
                        valueField: 'biz_id',
                        textField: 'value',
                        panelHeight: 'auto',
                        onChange: function (value) {

                            if (value == '') {
                                dg.datagrid('removeFilterRule', 'biz_id');
                            } else {
                                dg.datagrid('addFilterRule', {
                                    field: 'biz_id',
                                    op: 'equal',
                                    value: value
                                });
                            }
                            dg.datagrid('doFilter');
                        }
                    },
                    op: 'equal'
                }
                , {
                    field: 'old_table_nm',
                    type: 'combobox',
                    options: {
                        url: '/dimTrans/getOldTableNameList',
                        prompt: '原表名',
                        method: 'get',
                        valueField: 'name',
                        width: 200,
                        textField: 'name',
                        panelHeight: 'auto',
                        onChange: function (value) {

                            if (value == '') {
                                dg.datagrid('removeFilterRule', 'old_table_nm');
                            } else {
                                dg.datagrid('addFilterRule', {
                                    field: 'old_table_nm',
                                    op: 'equal',
                                    value: value
                                });
                            }
                            dg.datagrid('doFilter');
                        }
                    },
                    op: 'equal'
                }
            ]);
        });

        var editIndex = undefined;

        function endEditing() {
            if (editIndex == undefined) {
                return true;
            }

            if ($('#dg').datagrid('validateRow', editIndex)) {
                // 获取编辑器并更改它的值

                var ed = $('#dg').datagrid('getEditor', {index: editIndex, field: 'biz_id'});
                var value = $(ed.target).combobox('getText');


                $('#dg').datagrid('getRows')[editIndex]['value'] = value;
                console.log(value);

                $('#dg').datagrid('endEdit', editIndex);
                editIndex = undefined;
                console.log("endEditing"+true);
                return true;
            } else {
                console.log("endEditing"+false);
                return false;
            }
        }

        function comboboxOnSelect (record){

//             var newPtion = record.combobox('getValue');
//             var newPtion1 = record.combobox('getText');
//             console.log(record);
//             console.log(newPtion);
//
//             console.log(newPtion1);

        }

        function onClickRow(index, row) {

            if (editIndex != index) {

                var dg = $('#dg');


             var _row = dg.datagrid('getSelected', editIndex); //得到原来选择的行

//                console.log("_row:"+_row.biz.biz_id);
//                console.log("_row.biz_id:"+_row.biz_id);
//
//
                dg.datagrid('endEdit', editIndex);   //将原来的行置于非编辑状态
                dg.datagrid('cancelEdit', editIndex); //将原来的行置于非编辑状态

                $('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);

               var ed = $('#dg').datagrid('getEditor', {index: index, field: 'biz_id'});
              // var value = $(ed.target).combobox('getText');
//                console.log(ed);
                //var ed = $('#dg').datagrid('getEditor', {index: editIndex, field: 'biz_id'});
                //var value = $(ed.target).combobox('getText');
                //console.log(ed);
                $(ed.target).combobox('setValue',_row.biz.biz_id);

                editIndex = index;

            }
        }

        function onClickRow1(index, row) {
            if (editIndex != index) {
                if (endEditing()) {

                    $('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);
                    editIndex = index;
                } else {
                    $('#dg').datagrid('selectRow', editIndex);
                    editIndex = index;
                }
            }
        }

        function onCheck(index, row) {
            if (editIndex != index) {
                if (endEditing()) {
                    $('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);
                    editIndex = index;
                } else {
                    $('#dg').datagrid('selectRow', editIndex);
                    editIndex = index;
                }
            }
        }
        function reject() {
            $('#dg').datagrid('rejectChanges');
            editIndex = undefined;
        }

        function addNewRow() {
            var dg = $('#dg');

            var _row = dg.datagrid('getSelected', editIndex); //得到当前选择的行
            dg.datagrid('endEdit', editIndex);   //将当前行置于非编辑状态
            dg.datagrid('cancelEdit', editIndex);
            $('#dg').datagrid('uncheckRow', editIndex);

            editIndex = editIndex + 1;//指定要编辑的行为选择的行加1

            if (editIndex != undefined) {//在选择的节点下添加行


                $('#dg').datagrid('insertRow', {//在指定行添加数据，appendRow是在最后一行添加数据

                    index: editIndex,
                    row: {
                     old_table_nm: _row.old_table_nm
                    , old_column_nm1: _row.old_column_nm1
                    , old_key_id1: _row.old_key_id1
                    , old_key_nm1: _row.old_key_nm1
                    , new_table_nm: _row.new_table_nm
                    , new_column_nm: _row.new_column_nm
                    , new_key_id: _row.new_key_id
                    , new_key_nm: _row.new_key_nm
                    , biz_id: _row.biz.biz_id
                    }
                });

                $('#dg').datagrid('selectRow', editIndex);
                $('#dg').datagrid('beginEdit', editIndex);
            } else {
                alert("请选择要插入的位置")
            }

        }

        function saveOrUpdateRow(index, row) {
            var dg = $('#dg');
            dg.datagrid('selectRow', editIndex);  ///将当前行置于选中状态
            var _row = dg.datagrid('getSelected', editIndex); //得到当前选择的行
            //console.log(_row);
            dg.datagrid('endEdit', editIndex);   //将当前行置于非编辑状态
            var data = null;
            //开始封装数据
            if (_row != undefined && _row != -1) {
                var data = {
                    id: _row.id
                    , old_table_nm: _row.old_table_nm
                    , old_column_nm1: _row.old_column_nm1
                    , old_key_id1: _row.old_key_id1
                    , old_key_nm1: _row.old_key_nm1
                    , new_table_nm: _row.new_table_nm
                    , new_column_nm: _row.new_column_nm
                    , new_key_id: _row.new_key_id
                    , new_key_nm: _row.new_key_nm
                    , biz_id: _row.biz_id
                }
            } else {
                var data = {
                    old_table_nm: _row.old_table_nm
                    , old_column_nm1: _row.old_column_nm1
                    , old_key_id1: _row.old_key_id1
                    , old_key_nm1: _row.old_key_nm1
                    , new_table_nm: _row.new_table_nm
                    , new_column_nm: _row.new_column_nm
                    , new_key_id: _row.new_key_id
                    , new_key_nm: _row.new_key_nm
                    , biz_id: _row.biz_id
                }
            }

            var _url = "/dimTrans/save"
            ajaxRequest(_url, data);
            $('#dg').datagrid('reload');   //刷新当前页面的数
        }

        function ajaxRequest(url, data, callBack) {//callBack为处理请求返回结果的回调函数

            //jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)
            console.log("data:" + data);
            $.post(url, data, function (result) {//成功是的回调函数
                if (callBack != undefined) { //判断是否传入回调函数
                    callBack(result);
                } else {
                    commonResult(result);   //如果没有传入回调函数，就使用通用的办法处理
                }
            });
        }


        function deleteRows() {
            var checkedItems = $('#dg').datagrid('getChecked');

            var names = [];

            $.each(checkedItems, function (index, item) {

                names.push(item.id);
            });

            //console.log(names);
            if (names.length == 0) {
                alert("没有选择要删除的行");
            } else {
                var data = {
                    ids: names
                }
                var _url = "/dimTrans/deleteByIds"
                ajaxRequest(_url, data);
            }
        }

        function commonResult(result_data) {
            var result = jQuery.parseJSON(result_data);

            if (result.code == 1) {
                editIndex = undefined;

                alert("处理数据成功!");
            } else {
                editIndex = undefined;
                alert("处理数据失败!");
            }
            $('#dg').datagrid('reload');   //刷新当前页面的数
        }

        function cancelEdit() {
            if (editIndex != undefined && editIndex != -1) {
                $("#dg").datagrid('endEdit', editIndex);//结束编辑，传入之前编辑的行
                $('#tg').datagrid('cancelEdit', editIndex);
                $('#tg').datagrid("reload");
                // editIndex = undefined;
            } else {
                $('#tg').datagrid('remove', -1);
                //editIndex = undefined;
            }
        };
        function reject() {
            $('#dg').datagrid('rejectChanges');
            editIndex = undefined;
        }

        function getChanges() {
            var rows = $('#dg').datagrid('getChanges');
            alert(rows.length + ' rows are changed!');
        }

        function getValue(value, row, index) {
            if (row.biz) {
                return row.biz.value;
            }
            return value;
            //return biz != null ? biz.value : "";
        }


    </script>


    <div id="tb" style="padding:5px;height:auto">
        <div style="margin-bottom:5px">
            <a href="#" id="addBtn" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addNewRow()"
               title="添加"></a>
            <a href="#" id="saveBtn" class="easyui-linkbutton" iconCls="icon-save" plain="true"
               onclick="saveOrUpdateRow()" title="保存"></a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true"
               onclick="reject()" title="撤销" plain="true"></a>
            <a href="#" id="deleteBtn" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
               onclick="deleteRows()" title=“删除”></a>
            <!--<a href="#" id="modifyBtn" class="easyui-linkbutton" iconCls="icon-edit" plain="true" title=“编辑”></a>-->


            <!--<a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true"></a>-->

        </div>
        <!--        <form id="form-auto-filter" method="post">
                    <div>

                        <input id="old_table_nm" class="easyui-combobox"
                               name="old_table_nm"
                               data-options="
                            url:'/dimTrans/getOldTableNameList',
                            prompt:'源表名',
                            method:'get',
                            valueField:'code',
                            width:200,
                            textField:'name',
                            panelHeight:'auto'
                    ">

                        <input id="new_table_nm" class="easyui-combobox"
                               name="new_table_nm"
                               data-options="
                            url:'/dimTrans/getNewTableNameList',
                            method:'get',
                            prompt:'新表名',
                            valueField:'code',
                            width:150,
                            textField:'name',
                            panelHeight:'auto'
                    ">

                        <input id="new_column_nm" class="easyui-combobox"
                               name="new_column_nm"
                               data-options="
                            url:'/dimTrans/getNewColumnNameList',
                            method:'get',
                            prompt:'新字段名',
                            width:150,
                            valueField:'code',
                            textField:'name',
                            panelHeight:'auto'
                    ">

                        <input id="biz_id" class="easyui-combobox"
                               name="biz_id"
                               data-options="
                            url:'/rule/getRuleDepartment_1_2',
                            method:'get',
                            prompt:'业务',
                            width:150,
                            valueField:'code',
                            textField:'value',
                            panelHeight:'auto'
                    ">
                        <a href="#" class="easyui-linkbutton" iconCls="icon-search">Search</a>
                    </div>

                </form>-->
    </div>
</div>

<script type="text/javascript">

</script>
</body>
</html>